package com.frank.loadinglibrary.base.baseelement;

import com.frank.loadinglibrary.base.baseanimations.BaseAnimation;
import com.frank.loadinglibrary.base.baseanimations.BaseAnimationController;
import ohos.agp.animation.Animator;
import ohos.agp.render.Canvas;

/**
 * @author eric_shaw
 * @version 1.0
 * @description 组件元素基类
 * @classname BaseElement.class
 * @program loadinglibrary
 * @date 2021/06/09
 **/
public abstract class BaseElement implements BaseAnimation, IViewElement {

    protected float mWidth;

    protected float mHeight;

    /**
     * 组件回调接口
     */
    protected IComponentCallback callback;

    /**
     * 动画控制类
     */
    private BaseAnimationController<Animator> controller;


    public BaseAnimationController getController() {
        if (controller == null) {
            if (initAnimationController() == null) {
                throw new RuntimeException("must init IAnimationController.");
            }
            controller = new BaseAnimationController(initAnimationController());
        }
        return controller;
    }

    /**
     * 初始化动画控制类并返回
     *
     * @return
     */
    public abstract BaseAnimationController.IAnimationController initAnimationController();

    @Override
    public void startAnimators() {
        if (getController() != null) {
            getController().startAnimators();
        }
    }

    @Override
    public void stopAnimators() {
        if (getController() != null) {
            getController().stopAnimators();
        }
    }

    @Override
    public void pauseAnimation() {
        if (getController() != null) {
            getController().pauseAnimation();
        }
    }

    @Override
    public void resumeAnimation() {
        if (getController() != null) {
            getController().resumeAnimation();
        }
    }

    @Override
    public void resetAnimation() {
        if (getController() != null) {
            getController().resetAnimation();
        }
    }

    @Override
    public boolean isAnimationRunning() {
        if (getController() != null) {
            return getController().isAnimationRunning();
        }
        return false;
    }


    @Override
    public void initPaint() {

    }

    @Override
    public void draw(Canvas canvas) {

    }


    @Override
    public void onMeasure(float width, float height) {
        this.mWidth = width;
        this.mHeight = height;
    }

    @Override
    public void release() {
        if (controller != null) {
            controller.release();
        }
    }

    public void reflushComponent() {
        if (callback != null) {
            callback.reflushComponent();
        }
    }

    public float getWidth() {
        return this.mWidth;
    }

    public float getHeight() {
        return this.mHeight;
    }
}
